package managers.dateManager.endFrozen.beginChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.constants.TimeConstants;
import org.tbjd.client.exceptions.TaskManagerException;
import org.tbjd.client.managers.TaskManager;
import org.tbjd.client.states.StateBeginFrozenASAP;

import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods when BeginDate changed
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * A task is in a phase, when the BeginDate of the phase change, the task dates are updated
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * BeginDate of the phase "f1" is changed
 * </p>
 * <p>
 * Expected
 * <ul>
 * <li>update BeginDate/EndDate of phase "f1"</li>
 * <li>update BeginDate/EndDate of "task"</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class EndFrozen_BeginChange_UpdatePhase_Basic extends TestCase {

	/*
	 * Task		Begins 		Ends 		Predecessor Successor 	ChainingMode	Parent		task +2 days
	 * f1 		15.6.2009 	18.6.2009 	-	 		-		 	-	 			-			17.6.2009 - 20.6.2009
	 * task		15.6.2009 	18.6.2009 	-			-			ASAP 			f1			17.6.2009 - 20.6.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task f1 = null;
	private TaskManager dateManager = new TaskManager();
	private static final long ADDED_DAYS = 2;

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		this.f1 = TaskFactory.getInstance();
		this.f1.setName("f1");
		f1.setBeginDate(df.parse("15.6.2009"));
		f1.setEndDate(df.parse("18.6.2009"));
		this.f1.setType(Task.PHASE);

		Task task = TaskFactory.getInstance();
		task.setName("task");
		task.setBeginDate(df.parse("15.6.2009"));
		task.setEndDate(df.parse("18.6.2009"));
		task.setDuration(72);
		task.setState(new StateBeginFrozenASAP(task.getState().getDurationModeState()));

		this.f1.addChild(task);
		task.setParent(f1);

		super.setUp();
	}

	public void testReturnedBeanListNotNull() {
		System.out.println("");
		log.debug(" === testReturnedBeanListNotNull");

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("1.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.f1);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testReturnedListSizeIsOk() {
		System.out.println("");
		log.debug(" === testReturnedListSizeIsOk");

		Date newBeginDate = null;
		try {
			newBeginDate = df.parse("6.1.2009");

		} catch (ParseException e1) {
			fail(e1.getMessage());

		}
		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.f1);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 2", 2, updatedBeans.size());

	}

	public void testReturnedDatesAreOk() {
		System.out.println("");
		log.debug(" === testReturnedDatesAreOk");

		Date newBeginDate = new Date(this.f1.getBeginDate().getTime() + (ADDED_DAYS * TimeConstants.DAY_IN_MILLIS));

		log.debug("new BeginDate = " + df.format(newBeginDate));

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.DATEMANAGER_ACTION_beginDate, newBeginDate);
		this.map.put(NotificationsConstants.DATEMANAGER_BEAN, this.f1);
		this.map.put(NotificationsConstants.DATEMANAGER_SENDER, null);

		log.debug("call dateManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager .manageDates(this.map);

		} catch (TaskManagerException e) {
			fail(e.getMessage());

		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("f1")) {
					assertEquals("check task BeginDate", df.parseObject("17.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("20.6.2009"), updatedBean.getEndDate());
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("17.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("20.6.2009"), updatedBean.getEndDate());
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}
}